package top.xxyu;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
import org.apache.commons.lang3.StringUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class SSHWindowService {

    private Connection connection;
    private Session session;


    /**
     * 创建链接
     * @param ip
     * @return
     */
    public void connect(String ip){
       this.connection = new Connection(ip);
        try {
            connection.connect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 登录
     * @param userName
     * @param passWord
     * @return
     */
    public boolean isAuthenticated(String userName, String passWord){
        //利用用户名和密码进行授权
        boolean isAuthenticated = false;
        try {
            isAuthenticated = connection.authenticateWithPassword(userName, passWord);
            if(isAuthenticated ==false){
                System.out.println("Authorication failed");
                throw new IOException("Authorication failed");
            }
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }


    public String execConmand(String command){
        //打开会话,执行命令
        InputStream stdout = null;
        BufferedReader br = null;
        StringBuilder console = new StringBuilder();
        try {
            session = connection.openSession();
            session.requestPTY("vt100", 80, 24, 640, 480, null);
            try {
                Thread.sleep(4000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if(StringUtils.isNotEmpty(command)) {
                String encodeSet = "";
                session.execCommand(encodeSet+command);
                session.execCommand(command);
                stdout = new StreamGobbler(session.getStdout());
                br = new BufferedReader(new InputStreamReader(stdout));
                while(true){
                    String line = br.readLine();
                    if(line==null) break;
                    System.out.println(line);
                    console.append(line);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(null != br) {
                    br.close();
                }
                if(null != stdout) {
                    stdout.close();
                }
                if(null != session) {
                    session.close();
                }
                if(null != connection) {
                    connection.close();
                }
            } catch (IOException e) {

                e.printStackTrace();
            }
        }
        return console.toString();
    }

    public void close(){
        session.close();
        connection.close();
    }


    public Connection getConnection() {
        return connection;
    }

    public void setConnection(Connection connection) {
        this.connection = connection;
    }

    public Session getSession() {
        return session;
    }

    public void setSession(Session session) {
        this.session = session;
    }
}
